package com.stu.service.impl;

import com.stu.config.VcenterConfig;
import com.stu.service.TestVmwareApiService;
import com.vmware.vim25.mo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

/**
 * @projectName: learn_used_code
 * @packageName: com.stu.service.impl
 * @className: com.stu.service.impl-> TestServiceInstance
 * @createDate: 2021-11-19 18:25
 * @author: ZhangSheng
 * @version: 1.0
 * @description:
 */
@Slf4j
@Service
public class TestServiceInstance implements TestVmwareApiService {

    @Autowired
    private VcenterConfig vcenterConfig;

    private ServiceInstance serviceInstance;

    private HostSystem[] resources;

    private VirtualMachine[] vms;

    private ResourcePool[] resourcePools;

    private Datacenter[] datacenters;

    private ClusterComputeResource[] clusterComputeResources;

    private Datastore[] datastores;

    private Network[] networks;

    @Override
    public ServiceInstance connection() throws MalformedURLException, RemoteException {
        URL url = new URL("https", vcenterConfig.getIp(),
                null != vcenterConfig.getPort() ? vcenterConfig.getPort() : 443, "/sdk");
        this.serviceInstance = new ServiceInstance(url, vcenterConfig.getUserName(),vcenterConfig.getPassword(),true,30 * 1000, 60 * 1000);
        return this.serviceInstance;
    }

    @Override
    public ServiceInstance getServiceInstance(){
        return this.serviceInstance;
    }

    @Override
    public HostSystem[] getHostSystemList() throws RemoteException {
        if (resources != null && resources.length > 0){
            return this.resources;
        }
        ManagedEntity[] entities = new InventoryNavigator(serviceInstance.getRootFolder()).searchManagedEntities("HostSystem");
        HostSystem[] resources = new HostSystem[entities.length];
        for (int i = 0; i < entities.length; i++) {
            resources[i] = (HostSystem) entities[i];
        }
        this.resources = resources;
        log.info("获取到宿主机数量 {} 台", entities.length);
        return this.resources;
    }

    @Override
    public VirtualMachine[] getVirtualMachineList() throws RemoteException {
        if (this.vms != null && this.vms.length > 0){
            return this.vms;
        }
        ManagedEntity[] entities = new InventoryNavigator(serviceInstance.getRootFolder()).searchManagedEntities("VirtualMachine");
        VirtualMachine [] vms = new VirtualMachine[entities.length];
        for (int i = 0; i < entities.length; i++) {
            vms[i] = (VirtualMachine)entities[i];
        }
        this.vms = vms;
        log.info("获取到的虚机数量 {} 台;", this.vms.length);
        return this.vms;

    }

    @Override
    public ResourcePool[] getResourcePoolList() throws RemoteException {
        if (this.resourcePools != null && this.resourcePools.length > 0){
            return  this.resourcePools;
        }
        ManagedEntity[] entities = new InventoryNavigator(serviceInstance.getRootFolder()).searchManagedEntities("ResourcePool");
        ResourcePool [] resourcePools = new ResourcePool[entities.length];
        for (int i = 0; i < entities.length; i++) {
            ResourcePool entity = (ResourcePool)entities[i];
            resourcePools[i] = entity;
        }
        this.resourcePools = resourcePools;
        log.info("获取到资源池数量 {} 个",  this.resourcePools.length);
        return this.resourcePools;
    }

    @Override
    public Datacenter[] getDatacenterList() throws RemoteException {
        if (this.datacenters != null && this.datacenters.length > 0){
            return this.datacenters;
        }
        ManagedEntity[] datacenters = new InventoryNavigator(serviceInstance.getRootFolder()).searchManagedEntities("Datacenter");
        Datacenter[] datacentersList = new Datacenter[datacenters.length];
        for (int i = 0; i < datacenters.length; i++) {
            datacentersList[i] = (Datacenter)datacenters[i];
        }
        this.datacenters = datacentersList;
        log.info("获取到数据中心数量：{} 个",this.datacenters.length);
        return this.datacenters;
    }

    @Override
    public ClusterComputeResource[] getClustComputeResourceList() throws RemoteException {
        if (clusterComputeResources != null && clusterComputeResources.length > 0){
            return this.clusterComputeResources;
        }
        ManagedEntity[] clusterComputeResources = new InventoryNavigator(serviceInstance.getRootFolder()).searchManagedEntities("ClusterComputeResource");
        ClusterComputeResource[] clusterComputeResourceList = new ClusterComputeResource[clusterComputeResources.length];
        for (int i = 0; i < clusterComputeResources.length; i++) {
            clusterComputeResourceList[i] = (ClusterComputeResource)clusterComputeResources[i];
        }
        this.clusterComputeResources = clusterComputeResourceList;
        log.info("获取到集群计算资源数量：{} 个",this.clusterComputeResources.length);
        return this.clusterComputeResources;
    }

    @Override
    public Datastore[] getDatastoreList() throws RemoteException {
        if (datastores != null && datastores.length > 0){
            return this.datastores;
        }
        ManagedEntity[] datastores = new InventoryNavigator(serviceInstance.getRootFolder()).searchManagedEntities("Datastore");
        Datastore[] datastoreList = new Datastore[datastores.length];
        for (int i = 0; i < datastores.length; i++) {
            datastoreList[i] = (Datastore)datastores[i];
        }
        this.datastores = datastoreList;
        log.info("获取到数据存储数量 {} 个",this.datastores.length);
        return this.datastores;
    }

    @Override
    public Network[] getNetworks() throws RemoteException {
        if (networks != null && networks.length > 0){
            return this.networks;
        }
        ManagedEntity[] datastores = new InventoryNavigator(serviceInstance.getRootFolder()).searchManagedEntities("Network");
        Network[] networkList = new Network[datastores.length];
        for (int i = 0; i < datastores.length; i++) {
            networkList[i] = (Network)datastores[i];
        }
        this.networks = networkList;
        log.info("获取到网络数量 {} 个",this.datastores.length);
        return this.networks;
    }


}
